<?php

require_once('../../config/maptrick.conf.php');
require_once(SITE_ROOT . 'lib/PHT.php');
require_once(SITE_ROOT . 'class/bean/Country.class.php');
require_once(SITE_ROOT . 'class/db/DBCountry.class.php');
require_once(SITE_ROOT . 'class/db/DBHCountry.class.php');

try {
	if ($_GET["pass"] != SCRIPT_PWD) die("Incorrect password");

	$HT = new CHPPConnection(CONSUMERKEY, CONSUMERSECRET);
	$HT->setOauthToken(USERTOKEN);
	$HT->setOauthTokenSecret(USERTOKENSECRET);
	
	$leagues = $HT->getWorldDetails();
	$countries = new ArrayObject();
	for ($i=1; $i<=$leagues->getLeagueNumber(); $i++) {
		$league = $leagues->getLeague($i);
		$country = new Country();
		$country->setCountryID($league->getId());
		$country->setName($league->getName());
		$country->setEnglishName($league->getEnglishName());
		$country->setDivisions($league->getNumberLevel());
		$country->setSeasons($league->getSeasonNumber());
		$country->setActiveTeams($league->getNumberActiveTeams());
		$country->setActiveUsers($league->getNumberActiveUsers());
		$country->setWaitingUsers($league->getNumberWaitingUsers());
		$country->setDate(time());
		$countries->append($country);
	}
	$db = new DBCountry();
	$old_countries = $db->getCountries();
	$dbh = new DBHCountry();
	$dbh->saveCountries($old_countries);
	$db->saveCountries($countries);
	$db->updateCoolness($countries);
	echo date('Y-m-d', time()) . ' -  daily update successful';
}
catch (Exception $e) {
	echo $e->getMessage();
}

?>